package com.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;

public class VerticalTextView extends TextView {

    //The below two constructors appear to be required
    public VerticalTextView(Context context) {
        super(context);
        this.setBackgroundColor(Color.BLUE);
    }

    public VerticalTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    @Override
    public void onDraw(Canvas canvas) {
        //This saves off the matrix that the canvas applies to draws, so it can be restored later. 
        canvas.save(); 

        //now we change the matrix
        //We need to rotate around the center of our text
        //Otherwise it rotates around the origin, and that's bad. 
        float py = this.getHeight()/2.0f;
        float px = this.getWidth()/2.0f;
        
        canvas.rotate(-90, px, py);
        canvas.translate((this.getWidth()/2.0f)-(this.getHeight()/2.0f), (this.getHeight()/2.0f)-(this.getWidth()/2.0f));
        
        //int temp = this.getHeight();
        this.setHeight(this.getWidth());
        //this.setWidth(temp);

        //draw the text with the matrix applied. 
        super.onDraw(canvas); 

        //restore the old matrix. 
        canvas.restore(); 
    }
}
